package com.bibrApp.difu.actvity.wait_delivery_mvp;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.CoverShortResBody;
import com.bibrApp.difu.Response.OrderDetailResBody;
import com.bibrApp.difu.Response.OrderMoreDetailResBody;
import com.bibrApp.difu.Response.SignDataResBody;
import com.bibrApp.difu.Response.SignIVDataResBody;
import com.bibrApp.difu.actvity.connect_mvp.ConnectActivity;
import com.bibrApp.difu.actvity.order_detail_mvp.IOrderDetailView;
import com.bibrApp.difu.actvity.order_detail_mvp.OrderDetailPresenter;
import com.bibrApp.difu.actvity.wallet_mvp.WalletActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.fragment.jieru_mvp.JiedaiManagerBean;
import com.bibrApp.difu.utils.BigdecimalUtils;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.StrValidateUtils;
import com.bibrApp.difu.utils.Tools;

import java.math.RoundingMode;

import butterknife.BindView;
import butterknife.OnClick;

public class WaitDeliveryActivity extends BaseActivity<OrderDetailPresenter> implements IOrderDetailView {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_orderid)
    TextView tvOrderId;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_money_title)
    TextView tv_money_title;
    @BindView(R.id.tv_jc_money_title)
    TextView tv_jc_money_title;
    @BindView(R.id.tv_lx_title)
    TextView tv_lx_title;
    @BindView(R.id.tv_money)
    TextView tvTotleMoney;
    @BindView(R.id.tv_jc_money)
    TextView tvJrMoney;
    @BindView(R.id.tv_lx)
    TextView tvLixi;
    @BindView(R.id.tv_sxf)
    TextView tvShouxufei;
    @BindView(R.id.tv_drate)
    TextView tvDrate;
    @BindView(R.id.tv_yrate)
    TextView tvYrate;
    @BindView(R.id.tv_dyrate)
    TextView tvDyrate;
    @BindView(R.id.tv_cycle)
    TextView tvCycle;
    @BindView(R.id.tv_cdyrate)
    TextView tvCdyRate;
    @BindView(R.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R.id.tv_dy_count)
    TextView tvDyCount;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.ll_addr_sign_more)
    RelativeLayout llAddrSignMore;
    @BindView(R.id.tv_addr1)
    TextView tvAddr1;
    @BindView(R.id.tv_check1)
    TextView tv_check1;
    @BindView(R.id.iv_loading1)
    ImageView ivLoading1;
    @BindView(R.id.ll_addr_contract)
    RelativeLayout llAddrContract;
    @BindView(R.id.tv_addr2)
    TextView tvAddr2;
    @BindView(R.id.iv_loading2)
    ImageView ivLoading2;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.line_addr)
    View lineAddr;
    @BindView(R.id.tv_u_title)
    TextView tv_u_title;
    @BindView(R.id.tv_udun)
    TextView tv_udun;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.rl_top)
    RelativeLayout topRL;
    private int status;//8=补仓

    @Override
    protected void initPresenter() {
        presenter = new OrderDetailPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wait_delivery;
    }


    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void successOfGetDetail(OrderDetailResBody item) {
        if (item.getType() == 1) {//借入
            tvTitle.setText("抵押人缺钱借入");
            tv_jc_money_title.setText(String.format("借入金额(%s)", item.getPawnCoinEunit()));
        } else {//借出
            tvTitle.setText("投资人闲钱借出");
            tv_jc_money_title.setText(String.format("借出金额(%s)", item.getPawnCoinEunit()));
        }
        if (item.getStatus() == 8) {
            topRL.setVisibility(View.GONE);
            return;
        }
        tvOrderId.setText(String.format("订单号 %s", item.getOrderNo()));
        tvName.setText(String.format("对方姓名 %s", StrValidateUtils.replaceNameX(item.getOtherName())));
        tv_money_title.setText(String.format("本息合计(%s)", item.getPawnCoinEunit()));
        tv_lx_title.setText(String.format("到期利息(%s)", item.getPawnCoinEunit()));
        tvDrate.setText(String.format("%s%%", item.getLegalDailyRate() * 100));
        tvYrate.setText(String.format("%s%%", item.getLegalDailyRate() * 100 * 365));
        tvDyrate.setText(String.format("%s%%", item.getPawnRate() * 100));
        tvCdyRate.setText(String.format("%s%%", item.getPawnRate() * 100));
        tvJrMoney.setText(String.valueOf(item.getLegalQuantity()));
        if (item.getPawnCoinEunit().equalsIgnoreCase("btc")) {
//                tv_sxf.setText(item.get);
            tvLixi.setText(String.valueOf(BigdecimalUtils.getBigDecimalPointNum(item.getTotalInterest(), 8)));
            tvTotleMoney.setText(String.valueOf(BigdecimalUtils.getBigDecimalPrice(String.valueOf(item.getTotalInterest().add(item.getLegalQuantity())), 8)));
        } else {
            tvLixi.setText(String.valueOf(BigdecimalUtils.getBigDecimalPointNum(item.getTotalInterest(), 6)));
            tvTotleMoney.setText(String.valueOf(BigdecimalUtils.getBigDecimalPrice(String.valueOf(item.getTotalInterest().add(item.getLegalQuantity())), 6)));
        }
        tvCycle.setText(String.format("%d天", item.getCycle()));
        tvCoinType.setText(item.getPawnCoinEunit());
        //借贷金额/抵押率/实时币价
        tvDyCount.setText(String.valueOf(BigdecimalUtils.getBigDecimalPrice(String.valueOf(item.getLegalQuantity().divide(BigdecimalUtils.getBigDecimalPrice(String.valueOf(item.getLegalDailyRate()), 4), 8, RoundingMode.DOWN).divide(item.getNewPrice(), 8, RoundingMode.DOWN)), 8)));

    }

    /**
     * signWalletAddr	String	多签钱包地址
     * intelligentContractAddr	String	智能合约地址
     * mortgageContractAddr	String	抵押合约地址
     * fundUsdtAddr	String	投资人usdt地址
     * fundPawnCoinEunitAddr	String	平仓抵押人收入地址
     * pawnUsdtAddr	String	抵押人usdt地址
     * 0	待交割   1	交割中 2	待抵押
     * 3已抵押
     *
     * @param item
     */
    @Override
    public void successOfGetMoreDetail(OrderMoreDetailResBody item) {
        switch (item.getStatus()) {
            case 0:
                tv_status.setText("待交割");
                break;
            case 1:
                tv_status.setText("交割中");
                break;
            case 2:
                tv_status.setText("待抵押");
                break;
            default:
                tv_status.setText("已抵押");
                break;
        }

        tvAddr1.setText(item.getSignWalletAddr());
        tvAddr2.setText(item.getIntelligentContractAddr());
        if (item.getPawnCoinEunit().equalsIgnoreCase("btc")) {
            if (item.getType() == 1) {//借入
                tv_u_title.setText("请您在规定时间内向多重签名地址转入");
                tv_udun.setVisibility(View.VISIBLE);

            } else {//借出
                tv_udun.setVisibility(View.GONE);
                tv_u_title.setText("等待对方将资产转入多重签名地址");
            }
            tvInfo.setText(String.format("%s%s", String.valueOf(BigdecimalUtils.getBigDecimalPointNum(item.getPawnQuantity(), 8)), item.getPawnCoinEunit()));
        } else {
            //请您在规定时间内向资产智能合约地址转入
            if (item.getType() == 1) {//借入
                tv_udun.setVisibility(View.VISIBLE);
                tv_u_title.setText("请您在规定时间内向资产智能合约地址转入");

            } else {//借出
                tv_udun.setVisibility(View.GONE);
                tv_u_title.setText("等待对方将资产转入智能合约地址");
            }
            tvInfo.setText(String.format("%s%s", String.valueOf(BigdecimalUtils.getBigDecimalPointNum(item.getPawnQuantity(), 6)), item.getPawnCoinEunit()));
        }
        if (item.getStatus() < 3) {
            ivLoading1.setVisibility(View.VISIBLE);
            tv_check1.setVisibility(View.VISIBLE);
            checkAddr1();
            llAddrSignMore.setVisibility(View.VISIBLE);
        } else {
            ivLoading1.setVisibility(View.INVISIBLE);
            tv_check1.setVisibility(View.INVISIBLE);
            checkAddr2();
            tvCommit.setVisibility(View.VISIBLE);
            llAddrSignMore.setVisibility(View.VISIBLE);
            llAddrContract.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void successOfGetCoverShort(CoverShortResBody coverShortResBody) {
        topRL.setVisibility(View.GONE);
        tvInfo.setVisibility(View.GONE);
        if (coverShortResBody.getCoinEunit().equalsIgnoreCase("eth")) {
            tv_u_title.setText(String.format("请向智能合约地址转入至少%d个ETH", 1));
            llAddrContract.setVisibility(View.VISIBLE);
            checkAddr2();
        } else {
            tv_u_title.setText(String.format("请向多重签名地址转入至少%d个BTC", 1));
            llAddrSignMore.setVisibility(View.VISIBLE);
            checkAddr1();
        }
    }

    @Override
    public void successOfGetSignData(SignDataResBody signDataResBody) {

    }

    @Override
    public void successOfGetSignIVData(SignIVDataResBody signIVDataResBody) {

    }

    @Override
    public void initView() {
        JiedaiManagerBean bean = (JiedaiManagerBean) getIntent().getSerializableExtra("item");
        String orderNo = getIntent().getStringExtra("orderNo");
        status = getIntent().getIntExtra("status", 0);
        if (status == 8) {
            presenter.getCoverShort(orderNo);
        } else {
            if (bean == null) {
                presenter.getData(orderNo);
                presenter.getDataDetail(orderNo);
            } else {
                presenter.getData(bean.getId());
                presenter.getDataDetail(bean.getId());
            }
        }


    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        finish();
    }

    @OnClick(R.id.tv_guize1)
    public void xieyi1Click() {
        showToast("《多重签名资产共同管理规则》");
    }

    @OnClick(R.id.tv_guize2)
    public void xieyi2Click() {
        showToast("《智能合约规则》");
    }

    @OnClick(R.id.tv_udun)
    public void udunClick() {
        /*new DFDialog.Builder(this)
                .setTitle("恭喜您预借贷认购成功！")
                .setTitleVisible(View.GONE)
                .setMessage("\t尊敬的用户，请您确定您所绑定的BTC地址是否为U盾地址。若不是，请使用您所绑定的地址进行还款。")
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    checkAddr1();
                })
                .setNegativeButton("取消", (dialogInterface, i) -> {
                    finish();
                })
                .show();*/
        IntentUtil.toActivity(ConnectActivity.class, this);
    }

    private void checkAddr1() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(ivLoading1, "rotation", 0f, 360f);
        objectAnimator.setDuration(2000);
        objectAnimator.setRepeatMode(ValueAnimator.RESTART);
        objectAnimator.setRepeatCount(1000);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ivLoading1.clearAnimation();
                ivLoading1.setImageResource(R.mipmap.icon_ok);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        objectAnimator.start();
    }

    private void checkAddr2() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(ivLoading2, "rotation", 0f, 360f);
        objectAnimator.setDuration(2000);
        objectAnimator.setRepeatMode(ValueAnimator.RESTART);
        objectAnimator.setRepeatCount(1000);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ivLoading2.clearAnimation();
                ivLoading2.setImageResource(R.mipmap.icon_ok);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        objectAnimator.start();
    }


    @OnClick(R.id.tv_copy1)
    public void copy1Click() {
        String str = tvAddr1.getText().toString();
        Tools.copy(this, str);
    }

    @OnClick(R.id.tv_copy2)
    public void copy2Click() {
        String str = tvAddr2.getText().toString();
        Tools.copy(this, str);
    }

    @OnClick(R.id.tv_commit)
    public void rechargeUClick() {
        IntentUtil.toActivity(WalletActivity.class, this);
    }
}
